<template>
  <div class="app-container">
    <el-form ref="admin" :model="admin" :rules="rules" label-width="120px">
      <div style="width: 450px">
        <el-form-item :prop="admin.id === undefined ? 'username' : ''" label="管理员账号">
          <el-input v-model="admin.username" :disabled="admin.id!==undefined"/>
        </el-form-item>
        <el-form-item label="管理员密码" prop="password">
          <el-input v-model="admin.password" show-password/>
        </el-form-item>
        <el-form-item label="管理员昵称" prop="name">
          <el-input v-model="admin.name"/>
        </el-form-item>
      </div>
      <el-form-item label="管理员头像">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          class="avatar-uploader"
          action="http://localhost:8080/upload/">
          <img v-if="admin.avatar" :src="admin.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"/>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="addOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import admin from '../../api/admin'

export default {
  data() {
    const validateName = (rule, value, callback) => {
      // 后台方法
      admin.selectAdminByName(value).then(res => {
        if (res.data === true) {
          callback()
        } else {
          callback(new Error('用户名已存在'))
        }
      })
    }
    return {
      admin: {
        avatar: ''
      },
      saveBtnDisabled: false,
      rules: {
        username: [
          { required: true, message: '请输入用户账号', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入用户昵称', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户昵称', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    if (this.$route.params && this.$route.params.id) {
      const id = this.$route.params.id
      this.selectAdminById(id)
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.admin.avatar = res.data
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 20

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 20MB!')
      }
      return isJPG && isLt2M
    },
    addOrUpdate() {
      this.$refs['admin'].validate((valid) => {
        if (valid) {
          if (this.admin.id != null && this.admin.id.length > 0) {
            this.updateAdmin()
          } else {
            this.addAdmin()
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    addAdmin() {
      admin.addAdmin(this.admin).then(res => {
        this.$message.success(res.message)
        this.$router.push({ path: '/admin/list' })
      })
    },
    updateAdmin() {
      admin.updateAdmin(this.admin).then(res => {
        this.$message.success(res.message)
        this.$router.push({ path: '/admin/list' })
      })
    },
    selectAdminById(id) {
      admin.selectAdminById(id).then(res => {
        this.admin = res.data
      })
    }
  }
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
